package modes;

import java.io.File;
import lejos.nxt.Sound;
import main.ColorSensorChew;
import main.Engine;
import main.SensorEngine;
import main.SonicSensor;
import navigation.Direction;
import navigation.IllegalMoveException;
import navigation.Navigator;

/**
 * If read red
 * @author Max
 */
public class ModeRed extends AMode
{

    /**
     *
     * @param en Engine
     * @param se SensorEngine
     * @param cs ColorSensor
     * @param ss SonicSensor
     * @param navi Navigator
     */
    public ModeRed(Engine en, SensorEngine se, ColorSensorChew cs, SonicSensor ss, Navigator navi)
    {
        super(en, se, cs, ss, navi);
    }

    /**
     * Set a new´Navigator to find a exit
     * Turns around
     * @post read black
     * @return new Mode
     * @throws InterruptedException
     */
    public Mode run() throws InterruptedException
    {
        Mode mode = Mode.RED;
        engine.stop();
        rotationSensor.rotateMiddle();

        if (isReadMoreThanOnce())
        {
            Sound.playSample(new File("chewy1.wav"), 100);
            engine.rotateRight();
            for (int k = 0; k < 25; ++k)
            {
                color.readColor();
                if ((!color.isColorRed()) && (!color.isColorWhite()))
                {
                }
            }
            ModeController.dm.echo("End");
            addField();
            turn();
            engine.stop();
        }
        mode = Mode.BLACK;
        return mode;
    }

    private void turn() throws InterruptedException
    {
        engine.forward();
        engine.setSpeed(600);

        Thread.sleep(1500L);
        engine.rotateLeft();
        Thread.sleep(200L);

        do
        {
            color.readColor();
        } while (!color.isColorWhite());
        do
        {
            color.readColor();
        } while (color.isColorWhite());
    }

    private void addField()
    {
        try
        {
            navi.moveOneStep();
        } catch (IllegalMoveException ex)
        {
            ModeController.dm.echo(ex.toString());
        }
        navi.redPointFound();
        navi.directionChanged(Direction.BACKWARD);
        navi = config.Config.getGoToExitNavigator(navi);


    }

    private boolean isReadMoreThanOnce()
            throws InterruptedException
    {
        boolean readMoreThanOnce = true;
        int j = 0;
        engine.stop();
        engine.rotateRight();
        int count = 0;
        for (int k = 0; k < 25; ++k)
        {
            color.readColor();
            if ((!color.isColorRed()) && (!color.isColorWhite()))
            {
                engine.forward();
                readMoreThanOnce = false;
                break;
            }
            if (color.isColorRed())
            {
                count++;
            }
        }
        if (readMoreThanOnce)
        {
            engine.rotateLeft();
            for (int k = 0; k < 50; ++k)
            {
                color.readColor();
                if ((!color.isColorRed()) && (!color.isColorWhite()))
                {
                    engine.forward();
                    readMoreThanOnce = false;
                    break;
                }
                if (color.isColorRed())
                {
                    count++;
                }
            }
        }
        if (count < 4)
        {
            readMoreThanOnce = false;
        }
        return readMoreThanOnce;
    }

    Navigator getNavi()
    {
        return navi;
    }
}
